﻿using System;
using DapperExtensions.Mapper;
using Newtonsoft.Json;
using ZilLion.Service.Core.Domain.EntityBase.JsonConverter;

namespace ZilLion.Service.WeChatMall.Domain.Entitys.Orders
{
    public class OrderMainInfoDto
    {
        /// <summary>
        ///     ddlsh varchar(12) not null	''	npk 订单单据流水号
        /// </summary>
        [JsonProperty("sno")]
        public string Sno { get; set; }

        /// <summary>
        ///     dddjh varchar(15) not null	''			单据号
        /// </summary>
        [JsonProperty("bno")]
        public string Bno { get; set; }

        /// <summary>
        ///     dddjlx  tinyint not null	0			单据类型：1-订单，2-积分兑换订单
        /// </summary>
        [JsonProperty("selltype")]
        public SellType SellType { get; set; }

        /// <summary>
        ///     ddjffs  tinyint not null	0			交付方式：0-自提 1-配送
        /// </summary>
        [JsonProperty("deliverytype")]
        public DeliveryType DeliveryType { get; set; }

        /// <summary>
        ///     ddhsr   smalldatetime not null	''			核算日期(=制单保存时间的年月日)
        /// </summary>
        [JsonProperty("billdate")]
        [JsonConverter(typeof(ChinaDateTimeConverter))]
        public DateTime Billdate { get; set; }

        /// <summary>
        ///     ddwsckhid int not null	0			微商城顾客id
        /// </summary>
        [JsonProperty("custid")]
        public long Custid { get; set; }

        /// <summary>
        ///     ddkhzklbid  int not null	0			会员级别id
        /// </summary>
        [JsonProperty("memberlevel")]
        public long MemberLevel { get; set; }

        /// <summary>
        ///     ddhj    decimal(18,2)	not null	0			货品零售价合计/积分合计
        /// </summary>
        [JsonProperty("sellmy")]
        public decimal TotalMy { get; set; }

        /// <summary>
        ///     ddysjehj    decimal(18,2)	not null	0			应收：货品成交金额/积分合计
        /// </summary>
        [JsonProperty("dealmy")]
        public decimal DealMy { get; set; }

        /// <summary>
        ///     ddyspsfy    decimal(18,2)	not null	0			应收：配用费用
        /// </summary>
        [JsonProperty("feemy")]
        public decimal FeeMy { get; set; }

        /// <summary>
        ///     dddjbcsj datetime    not null	getdate()           单据保存时间
        /// </summary>
        [JsonProperty("updatedtime")]
        [JsonConverter(typeof(ChinaDateTimeConverter))]
        public DateTime UpdatedTime { get; set; }

        /// <summary>
        ///     shopid
        /// </summary>
        [JsonProperty("shopid")]
        public long Shopid { get; set; }

        /// <summary>
        ///     shopname
        /// </summary>
        [JsonProperty("shopname")]
        public string Shopname { get; set; }


        /// <summary>
        ///     奖励的积分
        /// </summary>
        [JsonProperty("rewardintegral")]
        public decimal RewardIntegral { get; set; }
    }

    public class PrePayOrderMainInfoEntity : OrderMainInfoDto
    {
    }

    public class PrePayOrderMainInfoEntityMapper : ClassMapper<PrePayOrderMainInfoEntity>
    {
        public PrePayOrderMainInfoEntityMapper()
        {
            Table("weiscdzfddt");
            Map(f => f.Sno).Column("ddlsh").Key(KeyType.Assigned);
            Map(f => f.Bno).Column("dddjh");
            Map(f => f.SellType).Column("dddjlx");
            Map(f => f.RewardIntegral).Column("ddzsjf");
            Map(f => f.DeliveryType).Column("ddjffs");
            Map(f => f.Billdate).Column("ddhsr");
            Map(f => f.Custid).Column("ddwsckhid");
            Map(f => f.MemberLevel).Column("ddkhzklbid");
            Map(f => f.TotalMy).Column("ddhj");
            Map(f => f.DealMy).Column("ddysjehj");
            Map(f => f.FeeMy).Column("ddyspsfy");
            Map(f => f.UpdatedTime).Column("dddjbcsj");
            Map(f => f.Shopid).Column("ddshopid");
            Map(f => f.Shopname).Column("ddshopname");
        }
    }

    public class OrderMainInfoEntity : OrderMainInfoDto
    {
        /// <summary>
        ///     "本次收：卡现金钱包
        ///     注：单据类型=积分兑换订单时：=0"
        /// </summary>
        [JsonProperty("crmcash")]
        public decimal CrmCash { get; set; }

        /// <summary>
        //"本次收：卡增值钱包（不含红包）
        //注：单据类型=积分兑换订单时：=0"
        /// </summary>
        [JsonProperty("crmincrement")]
        public decimal CrmIncrement { get; set; }

        /// <summary>
        //"本次收：卡红包
        //注：单据类型=积分兑换订单时：=0"
        /// </summary>
        [JsonProperty("crmred")]
        public decimal CrmRed { get; set; }

        //注：仅单据类型=积分兑换订单时存在，其他=0"
        /// <summary>
        ///     "本次收：积分
        /// </summary>
        [JsonProperty("crmintegral")]
        public decimal CrmIntegral { get; set; }

        /// <summary>
        ///     crm账户显示名称
        /// </summary>
        [JsonProperty("crmaccountname")]
        public string CrmAccountName { get; set; }

        /// <summary>
        ///     crm账户凭证类型：1-实体卡卡号 2-微信openid+"|"+appid(仅线上系统使用) 4-手机号 6-微信会员卡卡号
        /// </summary>
        [JsonProperty("crmaccounttype")]
        public short CrmAccountType { get; set; }

        /// <summary>
        ///     crm账户凭证号
        /// </summary>
        [JsonProperty("crmaccountvoucherno")]
        public string CrmAccountVoucherNo { get; set; }
    }


    public class OrderMainInfoEntityMapper : ClassMapper<OrderMainInfoEntity>
    {
        public OrderMainInfoEntityMapper()
        {
            Table("weiscddt");
            Map(f => f.Sno).Column("ddlsh").Key(KeyType.Assigned);
            Map(f => f.Bno).Column("dddjh");
            Map(f => f.SellType).Column("dddjlx");
            Map(f => f.DeliveryType).Column("ddjffs");
            Map(f => f.Billdate).Column("ddhsr");
            Map(f => f.Custid).Column("ddwsckhid");
            Map(f => f.MemberLevel).Column("ddkhzklbid");
            Map(f => f.TotalMy).Column("ddhj");
            Map(f => f.DealMy).Column("ddysjehj");
            Map(f => f.FeeMy).Column("ddyspsfy");
            Map(f => f.UpdatedTime).Column("dddjbcsj");
            Map(f => f.CrmCash).Column("ddkxjqb");
            Map(f => f.CrmIncrement).Column("ddkzzqb");
            Map(f => f.CrmRed).Column("ddkhb");
            Map(f => f.CrmIntegral).Column("ddkjf");
            Map(f => f.CrmAccountName).Column("ddcrmzhxsmc");
            Map(f => f.CrmAccountType).Column("ddcrmzhpzlx");
            Map(f => f.CrmAccountVoucherNo).Column("ddcrmzhpzh");
            Map(f => f.Shopid).Column("ddshopid");
            Map(f => f.Shopname).Column("ddshopname");
            Map(f => f.RewardIntegral).Column("ddzsjf");
        }
    }


    public enum DeliveryType
    {
        /// <summary>
        ///     自提
        /// </summary>
        Since = 0,

        /// <summary>
        ///     配送
        /// </summary>
        Send = 1
    }

    public enum SellType
    {
        /// <summary>
        ///     普通订单
        /// </summary>
        Order = 0,

        /// <summary>
        ///     积分兑换单
        /// </summary>
        Integral = 1
    }
}